package com.sanpu.kxe.business.member.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.sanpu.kxe.R;
import com.sanpu.kxe.business.main.bean.UserCenterOutput;
import com.sanpu.kxe.business.member.bean.MemberProductRateOutput;
import com.sanpu.kxe.common.interfaces.UrlConstants;
import com.sanpu.kxe.jump.imageloader.JKImageLoader;
import com.sanpu.kxe.module.roundview.RoundTextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;


/**
 * Description：会员产品费率
 * Created by 薛勇军 on 2019/4/15.
 */

public class MemberProductAdapter extends RecyclerView.Adapter<MemberProductAdapter.ViewHolder> {

    private Context mContext;

    private List<MemberProductRateOutput> mRateOutputs = new ArrayList<>();

    private UserCenterOutput mUserCenterOutput;

    public MemberProductAdapter(Context mContext, UserCenterOutput userCenterOutput) {
        this.mContext = mContext;
        this.mUserCenterOutput = userCenterOutput;
    }

    /**
     * 刷新数据
     */
    public void setNotifyData(List<MemberProductRateOutput> rateOutputs) {
        if (rateOutputs != null) {
            mRateOutputs.clear();
            mRateOutputs.addAll(rateOutputs);
        }
        this.notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_member_product, parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        MemberProductRateOutput rateOutput = mRateOutputs.get(position);
        String levelName = "";
        if (mUserCenterOutput != null) {
            if (!TextUtils.isEmpty(mUserCenterOutput.getLevelImagePath())) {
                JKImageLoader.getInstance().displayImage(mContext,
                        UrlConstants.getBaseUrl() + mUserCenterOutput.getLevelImagePath(), holder.gradeIv);
            }
            if (!TextUtils.isEmpty(mUserCenterOutput.getUserName())) {
                holder.accountNameTv.setText("账号：" + mUserCenterOutput.getUserName() + "");
            }
            levelName = mUserCenterOutput.getNickName();
        }
        if (rateOutput != null) {
            holder.levelNameTv.setText(levelName + "(" + rateOutput.getLevelName() + ")");
            if (!TextUtils.isEmpty(rateOutput.getAmount())) {
                holder.amountTv.setVisibility(View.VISIBLE);
                holder.amountTv.setText("" + rateOutput.getAmount() + "元");
            } else {
                holder.amountTv.setVisibility(View.GONE);
            }
            holder.descriptionTv.setText(!TextUtils.isEmpty(rateOutput.getDescription()) ? rateOutput.getDescription() : "");
            if (rateOutput.isButton()) {
                holder.upgradeRtv.setVisibility(View.VISIBLE);
                holder.upgradeRtv.setTag(rateOutput);
            } else {
                holder.upgradeRtv.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public int getItemCount() {
        if (mRateOutputs != null)
            return mRateOutputs.size();
        else
            return 0;
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        private ImageView gradeIv;

        private TextView levelNameTv, amountTv, descriptionTv, accountNameTv;

        private RoundTextView upgradeRtv;

        ViewHolder(View itemView) {
            super(itemView);
            levelNameTv = ButterKnife.findById(itemView, R.id.levelNameTv);
            amountTv = ButterKnife.findById(itemView, R.id.amountTv);
            descriptionTv = ButterKnife.findById(itemView, R.id.descriptionTv);
            accountNameTv = ButterKnife.findById(itemView, R.id.accountNameTv);
            upgradeRtv = ButterKnife.findById(itemView, R.id.upgradeRtv);
            gradeIv = ButterKnife.findById(itemView, R.id.gradeIv);
        }
    }
}
